/**
 * @author David Young
 */
package csab.game;


import org.newdawn.slick.Input;

import csab.core.BehaviorController;
import csab.core.Sprite;
/**
 * Meant to provide keyboard input to a Sprite
 * representing the player.
 * @author David Young
 * @see csab.game.sprites.PlayerSprite
 */
public class PlayerController implements BehaviorController {
	private Input lastinput;
	private Sprite player;
	
	/**
	 * Constructor for an object of this class.
	 * @param sprite The PlayerSprite that this controller
	 * will be attached to.
	 * @see csab.game.sprites.PlayerSprite
	 */
	public PlayerController(Sprite sprite)
	{
		player = sprite;
		lastinput = null;
	}
	
	/* (non-Javadoc)
	 * @see csab.core.BehaviorController#doBehavior(int, java.util.List, org.newdawn.slick.Input)
	 */
	@Override
	public void doBehavior(int delta, Sprite target, Input i) {
		if(!player.isDead())
		{
			lastinput = i;
			
			if(i.isKeyPressed(Input.KEY_W))
			{
				player.jump();
			}
			if(i.isKeyDown(Input.KEY_D))
			{
				player.moveRight();
			}
			else if(i.isKeyDown(Input.KEY_A))
			{
				player.moveLeft();
			}
			else if(i.isKeyDown(Input.KEY_S) && !player.isJumping())
			{
				player.idle();
			}
			else
			{
				player.stopMoving();
			}
		}
		else if(player.isDead())
			player.stopMoving();
	}
	
	/**
	 * Return the last Input object passed into
	 * doBehavior.
	 * @return The last Input object used.
	 */
	public Input getLastInput()
	{
		return lastinput;
	}

	/* (non-Javadoc)
	 * @see csab.core.BehaviorController#getOwnedSprite()
	 */
	@Override
	public Sprite getOwnedSprite() {
		return player;
	}

}
